package com.team1.system.stats.controller;

import com.team1.system.libraryborrow.domain.LibraryBorrow;
import com.team1.system.libraryborrow.service.ILibraryBorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
public class StatsBorrowReturnController {
    @Autowired
    private ILibraryBorrowService iLibraryBorrowService;

    //统计每个用户对每本书的借阅时长
    public Map<Long, Map<Long,Long>> BorrowTime(){
        // 此处要注意，去掉时分秒的差值影响，此处采用先换算为天再相减的方式
        final long ONE_DAY_MILLIS = 1000L * 60 * 60 * 24;
        LibraryBorrow libraryBorrow = new LibraryBorrow();
        List<LibraryBorrow> libraryBorrowList = iLibraryBorrowService.selectLibraryBorrowList(libraryBorrow);
        //创建map存储user_id对应的Book_id和该书的借书时间
        Map<Long,Map<Long, Long>> borrowtime = new TreeMap<>();
        for(LibraryBorrow lb : libraryBorrowList){
            Map<Long, Long> booktime = new TreeMap<>();
            //判断boorowtime内是否已经存在该用户
            for(Long key : borrowtime.keySet()){
                if(key.equals(lb.getUserId())){
                    booktime = borrowtime.get(lb.getUserId());
                    booktime.put(lb.getBookId(), Math.abs(lb.getReturnTime().getTime() / ONE_DAY_MILLIS - lb.getBorrowTime().getTime() / ONE_DAY_MILLIS) );
                }else{
                    booktime.put(lb.getBookId(), Math.abs(lb.getReturnTime().getTime() / ONE_DAY_MILLIS - lb.getBorrowTime().getTime() / ONE_DAY_MILLIS) );
                }
            }
            borrowtime.put(lb.getUserId(),booktime);
        }
        return borrowtime;
    }

    //统计每个用户总的借书时长
    public Map<Long,Long> UserAssembleTime(){
        long assembleTime = 0;
        Map<Long,Long> userassembletime = new TreeMap<>();
        Map<Long,Map<Long, Long>> borrowtime = BorrowTime();
        for(long key : borrowtime.keySet()){
            Map<Long,Long> booktime = borrowtime.get(key);
            for(long key_value : booktime.keySet()){
                assembleTime += booktime.get(key_value);
            }
            userassembletime.put(key,assembleTime);
            assembleTime = 0;
        }
        return userassembletime;
    }

    //每个用户的前十借书时长的图书id和对应的借书时长（排序，从大到小）
    public Map<Long,Map<Long,Long>> UserBookBorrowSortTime(){
        Map<Long,Map<Long,Long>> userbookborrowsorttime = new TreeMap<>();
        Map<Long,Map<Long, Long>> borrowtime = BorrowTime();
        for (long key : borrowtime.keySet()){
            //存放书籍时间排序
            Map<Long,Long> booksorttime = new LinkedHashMap<>();
            //获取原始数据
            Map<Long,Long> booktime = borrowtime.get(key);
            //对获取的数据进行排序
            ArrayList<Map.Entry<Long,Long>> list = new ArrayList<>(booktime.entrySet());
            Collections.sort(list, new Comparator<Map.Entry<Long, Long>>() {
                @Override
                public int compare(Map.Entry<Long, Long> o1, Map.Entry<Long, Long> o2) {
                    return - o1.getValue().compareTo(o2.getValue());
                }
            });
            //选取前十放入map
            int i = 0;
            for(Map.Entry<Long, Long> map : list){
                if(i < 10){
                    booksorttime.put(map.getKey(), map.getValue());
                    i++;
                }else {
                    break;
                }
            }
            userbookborrowsorttime.put(key,booksorttime);
        }
        return userbookborrowsorttime;
    }

    //所有用户的前十总借书时长的用户id和时长（排序，从大到小）
    public Map<Long,Long> UserBorrowSortTime(){
        //存放用户书籍总时间排序
        Map<Long,Long> userborrowsorttime = new LinkedHashMap<>();
        //获取原始数据
        Map<Long,Long> userassemble = UserAssembleTime();
        //对获取的数据进行排序
        ArrayList<Map.Entry<Long,Long>> list = new ArrayList<>(userassemble.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<Long, Long>>() {
            @Override
            public int compare(Map.Entry<Long, Long> o1, Map.Entry<Long, Long> o2) {
                return - o1.getValue().compareTo(o2.getValue());
            }
        });
        //选取前十放入map返回
        int i = 0;
        for(Map.Entry<Long, Long> map : list){
            if(i < 10){
                userborrowsorttime.put(map.getKey(), map.getValue());
                i++;
            }else {
                break;
            }
        }
        return userborrowsorttime;
    }

    //获取借书时长
    public double BorrowIdTime(String borrowid){
        final long ONE_DAY_MILLIS = 1000L * 60 * 60;
        LibraryBorrow libraryBorrow = iLibraryBorrowService.selectLibraryBorrowByBorrowId(borrowid);
        return (double) (libraryBorrow.getReturnTime().getTime() / ONE_DAY_MILLIS - libraryBorrow.getBorrowTime().getTime() / ONE_DAY_MILLIS);
    }
}
